home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / getword.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  2.4 KB  |  99 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: getword.c,v 5.1 1993/01/19 04:46:21 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1993 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: getword.c,v $
  16.  * Revision 5.1  1993/01/19  04:46:21  syd
  17.  * Initial Checkin
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include "defs.h"
  25.  
  26. int get_word(buffer, start, word, wordlen)
  27. char *buffer;
  28. int start;
  29. char *word;
  30. int wordlen;
  31. {
  32.     /*
  33.      * Extracts the next white-space delimited word from the "buffer"
  34.      * starting at "start" characters into the buffer and skipping any
  35.      * leading white-space there.  Handles backslash-quoted characters
  36.      * and double-quote bracked strings as an atomic unit.  The resulting
  37.      * word, up to "wordlen" bytes long, is saved in "word".  Returns the
  38.      * buffer index where extraction terminated, e.g. the next word can be
  39.      * extracted by starting at start+<return-val>.  If no words are found
  40.      * in the buffer then -1 is returned.
  41.      */
  42.  
  43.     register int len;
  44.     register char *p;
  45.  
  46.     for (p = buffer+start ; isspace(*p) ; ++p)
  47.     ;
  48.  
  49.     if (*p == '\0')
  50.     return (-1);        /* nothing IN buffer! */
  51.  
  52.     while (*p != '\0') {
  53.     len = len_next_part(p);
  54.     if (len == 1 && isspace(*p))
  55.         break;
  56.  
  57.     while (--len >= 0) {
  58.         if (--wordlen > 0)
  59.         *word++ = *p;
  60.         ++p;
  61.     }
  62.     }
  63.  
  64.     *word = '\0';
  65.     return (p - buffer);
  66. }
  67.  
  68.  
  69. #ifdef _TEST
  70. main()
  71. {
  72.     char buf[1024], word[1024], *bufp;
  73.     int start, len;
  74.  
  75.     while (gets(buf) != NULL) {
  76.  
  77.         puts("parsing with front of buffer anchored");
  78.         start = 0;
  79.         while ((len = get_word(buf, start, word, sizeof(word))) > 0) {
  80.             printf("start=%d len=%d word=%s\n", start, len, word);
  81.             start = len;
  82.         }
  83.         putchar('\n');
  84.  
  85.         puts("parsing with front of buffer updated");
  86.         bufp = buf;
  87.         while ((len = get_word(bufp, 0, word, sizeof(word))) > 0) {
  88.             printf("start=%d len=%d word=%s\n", 0, len, word);
  89.             bufp += len;
  90.         }
  91.         putchar('\n');
  92.  
  93.     }
  94.  
  95.     exit(0);
  96. }
  97. #endif
  98.  
  99.